package com.hzw.code.pageState

import android.content.Context
import android.graphics.Color
import android.view.ViewGroup
import androidx.annotation.LayoutRes
import com.hzw.code.R
import com.scwang.smartrefresh.layout.listener.OnRefreshListener

/**
 *
 * @Author:         HuaZhongWei
 * @CreateDate:     2020/4/16 16:41
 *
 * @Description:    空页面状态管理
 *
 */
class PageStateManager(builder: Builder) {

    val context: Context = builder.context

    val loadingLayoutResId = builder.loadingLayoutResId
    val contentLayoutResId = builder.contentLayoutResId

    val netErrorPageViewStub: PageViewStub? = builder.netErrorPageViewStub
    val emptyPageViewStub: PageViewStub? = builder.emptyPageViewStub
    val errorPageViewStub: PageViewStub? = builder.errorPageViewStub

    val onShowHideViewListener: OnShowHideViewListener? = builder.onShowHideViewListener

    val rootLayout: PageStateLayout

    init {

        rootLayout = PageStateLayout(builder.context)

        rootLayout.layoutParams = if (builder.isWrapContent) {
            ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
            )
        } else {
            ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
            )
        }

        rootLayout.setBackgroundColor(Color.WHITE)
        rootLayout.setStatusLayoutManager(this)
    }

    fun isShowLoading() = rootLayout.isLoading()

    fun showLoading() {
        if (!isShowLoading()) {
            rootLayout.showLoading()
        }
    }

    fun hideLoading() = rootLayout.hideLoading()

    fun showContentView() {
        rootLayout.showContent()
    }

    /**
     * 显示空数据，修改显示内容
     */
    fun showEmptyView(imageResId: Int = 0, tipText: String? = null, retryText: String? = null)  = rootLayout.showEmptyView(imageResId, tipText, retryText)

    /**
     * 显示网络错误，修改显示内容
     */
    fun showNetErrorView(imageResId: Int = 0, tipText: String? = null, retryText: String? = null)  = rootLayout.showNetErrorView(imageResId, tipText, retryText)

    /**
     * 显示错误界面，修改显示内容
     */
    fun showErrorView(imageResId: Int = 0, tipText: String? = null, retryText: String? = null) = rootLayout.showErrorView(imageResId, tipText, retryText)

    class Builder(val context: Context) {

        /**
         * StateFrameLayout布局创建时，是否包裹内容，默认是MATCH_PARENT
         */
        internal var isWrapContent = false

        internal var loadingLayoutResId = 0
        internal var contentLayoutResId = 0

        internal var netErrorPageViewStub: PageViewStub? = null
        internal var emptyPageViewStub: PageViewStub? = null
        internal var errorPageViewStub: PageViewStub? = null

        internal var onShowHideViewListener: OnShowHideViewListener? = null

        fun isWrapContent(isWrapContent: Boolean = false) =
            apply { this.isWrapContent = isWrapContent }

        /**
         * Loading页面
         */
        fun loadingLayoutResId(@LayoutRes loadingLayoutResId: Int = R.layout.layout_loading_default) =
            apply { this.loadingLayoutResId = loadingLayoutResId }

        /**
         * 内容页面
         */
        fun contentLayoutResId(@LayoutRes contentLayoutResId: Int) =
            apply { this.contentLayoutResId = contentLayoutResId }

        /**
         * 网络错误页面
         */
        fun netErrorView(
            @LayoutRes netErrorLayoutResId: Int = R.layout.layout_error_default,
            isEnableRefresh: Boolean = true,
            imageViewId: Int = R.id.errorImageView,
            imageResId: Int = R.drawable.ic_empty_01,
            tipsViewId: Int = R.id.errorTipsView,
            tipsText: String? = context.resources.getString(R.string.error_tips),
            retryViewId: Int = R.id.errorRetry,
            retryText: String? = context.resources.getString(R.string.retry),
            onRetryListener: OnRetryListener? = null,
            onRefreshListener: OnRefreshListener? = null
        ) = apply {
            this.netErrorPageViewStub = PageViewStub(context, netErrorLayoutResId)
            this.netErrorPageViewStub!!.isEnableRefresh = isEnableRefresh
            this.netErrorPageViewStub!!.imageViewId = imageViewId
            this.netErrorPageViewStub!!.imageResId = imageResId
            this.netErrorPageViewStub!!.tipsViewId = tipsViewId
            this.netErrorPageViewStub!!.tipsText = tipsText
            this.netErrorPageViewStub!!.retryViewId = retryViewId
            this.netErrorPageViewStub!!.retryText = retryText
            this.netErrorPageViewStub!!.onRetryListener = onRetryListener
            this.netErrorPageViewStub!!.onRefreshListener = onRefreshListener
        }

        /**
         * 空页面
         */
        fun emptyView(
            @LayoutRes emptyLayoutResId: Int = R.layout.layout_empty_default,
            isEnableRefresh: Boolean = true,
            imageViewId: Int = R.id.emptyImageView,
            imageResId: Int = R.drawable.ic_empty_01,
            tipsViewId: Int = R.id.emptyTipsView,
            tipsText: String? = context.resources.getString(R.string.empty_tips),
            retryViewId: Int = R.id.emptyRetry,
            retryText: String? = context.resources.getString(R.string.retry),
            onRetryListener: OnRetryListener? = null,
            onRefreshListener: OnRefreshListener? = null
        ) = apply {
            this.emptyPageViewStub = PageViewStub(context, emptyLayoutResId)
            this.emptyPageViewStub!!.isEnableRefresh = isEnableRefresh
            this.emptyPageViewStub!!.imageViewId = imageViewId
            this.emptyPageViewStub!!.imageResId = imageResId
            this.emptyPageViewStub!!.tipsViewId = tipsViewId
            this.emptyPageViewStub!!.tipsText = tipsText
            this.emptyPageViewStub!!.retryViewId = retryViewId
            this.emptyPageViewStub!!.retryText = retryText
            this.emptyPageViewStub!!.onRetryListener = onRetryListener
            this.emptyPageViewStub!!.onRefreshListener = onRefreshListener
        }

        /**
         * 网络错误页面
         */
        fun errorView(
            @LayoutRes errorLayoutResId: Int = R.layout.layout_error_default,
            isEnableRefresh: Boolean = true,
            imageViewId: Int = R.id.errorImageView,
            imageResId: Int = R.drawable.ic_empty_01,
            tipsViewId: Int = R.id.errorTipsView,
            tipsText: String? = context.resources.getString(R.string.error_tips),
            retryViewId: Int = R.id.errorRetry,
            retryText: String? = context.resources.getString(R.string.retry),
            onRetryListener: OnRetryListener? = null,
            onRefreshListener: OnRefreshListener? = null
        ) = apply {
            this.errorPageViewStub = PageViewStub(context, errorLayoutResId)
            this.errorPageViewStub!!.isEnableRefresh = isEnableRefresh
            this.errorPageViewStub!!.imageViewId = imageViewId
            this.errorPageViewStub!!.imageResId = imageResId
            this.errorPageViewStub!!.tipsViewId = tipsViewId
            this.errorPageViewStub!!.tipsText = tipsText
            this.errorPageViewStub!!.retryViewId = retryViewId
            this.errorPageViewStub!!.retryText = retryText
            this.errorPageViewStub!!.onRetryListener = onRetryListener
            this.errorPageViewStub!!.onRefreshListener = onRefreshListener
        }

        /**
         * 为状态View显示隐藏监听事件
         * @param listener                  listener
         * @return
         */
        fun onShowHideViewListener(listener: OnShowHideViewListener): Builder {
            onShowHideViewListener = listener
            return this
        }

        fun build(): PageStateManager {
            return PageStateManager(this)
        }
    }
}